/**
 * 自定义
 * */


var apis = {
    docIndex: {
        method: 'POST',
        url: apiDomain + '/doc/leftMenu'
    },
    docOne: {
        method: 'POST',
        url: apiDomain + '/doc/one'
    },
    docAdd: {
        method: 'POST',
        url: apiDomain + '/doc/add'
    }
};
var formArys = {
    id: 0,
    title: '',
    uri: '',
    reqbody: '',
    pamList: [],
    backbody: '',
    backList: []
};


//列表
function leftMenu(){
    id = id=='' ? 1 : id;
    $.ajax({
        type: apis.docIndex.method,
        url: apis.docIndex.url,
        data: {},
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var d = res.data;
            var h = '', i;
            for (i = 0; i < d.length; i ++) {
                if (id == d[i].id) {
                    h += '<div class="lm lmclick" onclick="window.location.href=\'/doc?id=' + d[i].id + '\';">';
                } else {
                    h += '<div class="lm" onclick="window.location.href=\'/doc?id=' + d[i].id + '\';">';
                }
                h += d[i].title;
                h += '</div>';
            }
            h += '<div class="lm">&nbsp;</div>';
            $('#leftMenu').html(h);
            $('#tongji').html('接口总数' + d.length + '个');
        },
        error: function (res) {
            console.log('错误啦');
        }
    });
}


//详情
function getOne(){
    $.ajax({
        type: apis.docOne.method,
        url: apis.docOne.url,
        data: { id:formArys.id },
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            formArys.title = res.data.title;
            formArys.uri = res.data.uri;
            formArys.reqbody = res.data.reqbody;
            formArys.pamList = res.data.pamList;
            formArys.backbody = res.data.backbody;
            formArys.backList = res.data.backList;
            var pams = res.data.pamList;
            var backs = res.data.backList;
            $('#uribody').html(res.data.uri);
            $('#reqbody').html(res.data.reqbody);
            $('#backbody').html(res.data.backbody);
            var h = '', i;
            if (pams.length > 0) {
                h = '';
                for (i = 0; i < pams.length; i ++) {
                    h += '<tr>';
                    h += '<td>' + pams[i].field_name + '</td>';
                    h += '<td>' + pams[i].field_type + '</td>';
                    h += '<td>' + pams[i].must + '</td>';
                    h += '<td class="remark">' + pams[i].remark + '</td>';
                    h += '</tr>';
                }
                $('#tReq > tbody').html(h);
            }
            if (backs.length > 0) {
                h = '';
                for (i = 0; i < backs.length; i ++) {
                    h += '<tr>';
                    h += '<td>' + backs[i].field_name + '</td>';
                    h += '<td>' + backs[i].field_type + '</td>';
                    h += '<td>' + backs[i].must + '</td>';
                    h += '<td class="remark" class="remark">' + backs[i].remark + '</td>';
                    h += '</tr>';
                }
                $('#tBack > tbody').html(h);
            }
        },
        error: function (res) {
            console.log('错误啦');
        }
    });
}


//新增接口
function formDocAdd(){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var h = '';
    h += '<style type="text/css"> #docAdd { position: fixed; top: 0; left: 0; z-index: 10000; } #docAdd .mask { width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000; } #docAdd .form { padding-bottom: 50px; width: 800px; min-height: 200px; max-height: 550px; background: #fff; overflow: auto; position: fixed; top: 50px; left: 15%; z-index: 10000; } #docAdd h4 { text-align: center; } #docAdd .group { margin: 10px 5%; width: 90%; color: #888; } #docAdd .group input { margin-top: 5px; padding: 10px 1%; width: 98%; border: 1px solid #eee; border-radius: 2px; background: #000; color: #fff; } #docAdd .group select { margin-top: 5px; padding: 10px 1%; width: 98%; border: 1px solid #eee; border-radius: 2px; } #docAdd .group textarea { margin-top: 5px; padding: 10px 1%; width: 98%; min-height: 100px; border: 1px solid #eee; border-radius: 2px; background: #000; color: #fff; resize: none; } #docAdd button { padding: 10px 0; width: 100%; border: 0; background: #0088CC; color: #EEEEEE; cursor: pointer; } #docAdd button:hover { color: #ffffff; background: #00a2d4; } #close { color: red; position: fixed; top: 60px; left: 920px; z-index: 10000; } </style>';
    h += '<div id="docAdd">';
    h += '<div class="mask"></div>';
    h += '<div class="form">';
    h += '<h4>新增接口</h4>';
    h += '<div class="group">';
    h += '<div>接口名：</div>';
    h += '<input type="text" id="title2" value=""/>';
    h += '</div>';
    h += '<div class="group">';
    h += '<div>接口地址：</div>';
    h += '<input type="text" id="uri2" value=""/>';
    h += '</div>';
    //h += '<div class="group">';
    //h += '<div>请求方法：</div>';
    //h += '<select id="method">';
    //h += '<option value="POST">POST</option>';
    //h += '<option value="GET">GET</option>';
    //h += '</select>';
    //h += '</div>';
    h += '<div class="group">';
    h += '<div>请求参数实例：</div>';
    h += '<textarea id="reqbody2"></textarea>';
    h += '</div>';
    h += '<div class="group">';
    h += '<div>返回数据实例：</div>';
    h += '<textarea id="backbody2"></textarea>';
    h += '</div>';
    h += '<div class="group">';
    h += '<button type="button" onclick="formSave();">保 存</button>';
    h += '</div>';
    h += '<a href="javascript:;" id="close" onclick="$(\'#formPop\').html(\'\');">关闭</a>';
    h += '</div>';
    h += '</div>';
    $('#formPop').html(h);
    formArys.id = 0;
}


//修改接口
function formDocEdit(){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var h = '';
    h += '<style type="text/css"> #docAdd { position: fixed; top: 0; left: 0; z-index: 10000; } #docAdd .mask { width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000; } #docAdd .form { padding-bottom: 50px; width: 800px; min-height: 200px; max-height: 600px; background: #fff; overflow: auto; position: fixed; top: 50px; left: 15%; z-index: 10000; } #docAdd h4 { text-align: center; } #docAdd .group { margin: 10px 5%; width: 90%; color: #888; } #docAdd .group input { margin-top: 5px; padding: 10px 1%; width: 98%; border: 1px solid #eee; border-radius: 2px; background: #000; color: #fff; } #docAdd .group select { margin-top: 5px; padding: 10px 1%; width: 98%; border: 1px solid #eee; border-radius: 2px; } #docAdd .group textarea { margin-top: 5px; padding: 10px 1%; width: 98%; min-height: 100px; border: 1px solid #eee; border-radius: 2px; background: #000; color: #fff; resize: none; } #docAdd button { padding: 10px 0; width: 100%; border: 0; background: #0088CC; color: #EEEEEE; cursor: pointer; } #docAdd button:hover { color: #ffffff; background: #00a2d4; } #close { color: red; position: fixed; top: 60px; left: 920px; z-index: 10000; } </style>';
    h += '<div id="docAdd">';
    h += '<div class="mask"></div>';
    h += '<div class="form">';
    h += '<h4>修改接口</h4>';
    h += '<div class="group">';
    h += '<div>接口名：</div>';
    h += '<input type="text" id="title2" value="' + formArys.title + '"/>';
    h += '</div>';
    h += '<div class="group">';
    h += '<div>接口地址：</div>';
    h += '<input type="text" id="uri2" value="' + formArys.uri + '"/>';
    h += '</div>';
    h += '<div class="group">';
    h += '<div>请求参数：</div>';
    h += '<textarea id="reqbody2">' + formArys.reqbody + '</textarea>';
    h += '</div>';
    h += '<div class="group">';
    h += '<div>返回数据：</div>';
    h += '<textarea id="backbody2">' + formArys.backbody + '</textarea>';
    h += '</div>';
    h += '<div class="group">';
    h += '<button type="button" onclick="formSave();">保 存</button>';
    h += '</div>';
    h += '<a href="javascript:;" id="close" onclick="$(\'#formPop\').html(\'\');">关闭</a>';
    h += '</div>';
    h += '</div>';
    $('#formPop').html(h);
}


//保存接口
function formSave(){
    var formData = {
        id: formArys.id,
        title: $('#title2').val(),
        uri: $('#uri2').val(),
        req: $('#reqbody2').val(),
        back: $('#backbody2').val()
    };
    //console.log(formData); return
    $.ajax({
        type: apis.docAdd.method,
        url: apis.docAdd.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg}); return
            }
            var operMsg = formArys.id==0 ? '添加' : '修改';
            msgPop({msg: operMsg + '成功'});
            setTimeout(function(){
                window.location.href = '';
            }, 1000);
        },
        error: function (res) {
            console.log('错误啦');
        }
    });
}


//自定义弹窗
function msgPop(e){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var msg = e.msg;
    var h = '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.5) + 'px; text-align: center; background: #000; color: #FFF; border-radius: 5px; position: fixed; bottom: 40%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">'+msg+'</div>';
    h += '</div>';
    h += '<script>setTimeout(function(){$(\'#msgPop\').html(\'\');},2000)</script>';
    $('#msgPop').html(h);
}


//获取地址栏中参数
function getParamByUrl(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r==null) return '';
    return  unescape(r[2]);
}